package com.xtoad.ecms.baseinfo.converter;

import com.xtoad.ecms.baseinfo.dto.PageElementDTO;
import com.xtoad.ecms.baseinfo.model.PageElement;
import com.xtoad.ecms.common.web.base.IConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * pageElement转换器实现类
 *
 * @author xtoad
 * @date 2020/05/29
 */
@Component
public class PageElementConverter implements IConverter<PageElementDTO, PageElement> {

    @Resource
    private WebPageConverter webPageConverter;

    /**
     * Model To DTO
     *
     * @param model 源
     * @return 目标
     */
    @Override
    public PageElementDTO toDto(PageElement model) {
        PageElementDTO dto = new PageElementDTO();
        dto.setId(model.getId());
        dto.setCreateTime(model.getCreateTime());
        dto.setCreateUser(model.getCreateUser());
        dto.setLastUpdateTime(model.getLastUpdateTime());
        dto.setLastUpdateUser(model.getLastUpdateUser());
        dto.setPage(webPageConverter.toDto(model.getPage()));
        dto.setType(model.getType());
        dto.setTitle(model.getTitle());
        return dto;
    }

    /**
     * DTO To Model
     *
     * @param dto 源
     * @return 目标
     */
    @Override
    public PageElement toModel(PageElementDTO dto) {
        PageElement model = new PageElement();
        model.setId(dto.getId());
        model.setCreateTime(dto.getCreateTime());
        model.setCreateUser(dto.getCreateUser());
        model.setLastUpdateTime(dto.getLastUpdateTime());
        model.setLastUpdateUser(dto.getLastUpdateUser());
        model.setPage(webPageConverter.toModel(dto.getPage()));
        model.setType(dto.getType());
        model.setTitle(dto.getTitle());
        return model;
    }

    /**
     * 批量Model To DTO
     *
     * @param models 源
     * @return 目标
     */
    @Override
    public List<PageElementDTO> toDtoList(List<PageElement> models) {
        List<PageElementDTO> dtoList = new ArrayList<>();
        models.forEach(model -> dtoList.add(this.toDto(model)));
        return dtoList;
    }

    /**
     * 批量DTO To Model
     *
     * @param dtos 源
     * @return 目标
     */
    @Override
    public List<PageElement> toModelList(List<PageElementDTO> dtos) {
        List<PageElement> modelList = new ArrayList<>();
        dtos.forEach(dto -> modelList.add(this.toModel(dto)));
        return modelList;
    }
}
